#ifndef SCT_MULTIMEDIA_AUDIOOUTPUT_H
#define SCT_MULTIMEDIA_AUDIOOUTPUT_H

#include <alsa/asoundlib.h>

#include "sct/core/common.h"
#include "sct/core/mutex.h"
#include "sct/core/thread.h"

#include "renderinterface.h"

namespace sct
{
class CAudioOutput: public CThreadCallBack, public CRenderInterface
{
public:
    CAudioOutput(const int sample_rate);
    virtual ~CAudioOutput();

    virtual void pushData(const void * d, const int len, const CDataInfo & info);
    virtual int64_t delay2Render();

protected:
    virtual int main(CThread * caller);
private:
    int size2frameNum(const int sz);
    int frameNum2size(const int num);
private:
    class CItem
    {
    public:
        uint8_t * data;
        int pos;
        int size;

    public:
        static void free(CItem * item)
        {
            SCT_DELETE_ARRAY(item->data);
            SCT_DELETE(item);
        }
    };

    enum State
    {
        Initing = SCT_ENUM_OFFSET,
        Playing,
        Terminated,
    };

private:
    CMutex * mMutex;
    std::list<CItem *> mDatas;
    int mSize;

    CThread * mThd;
    State mState;

    snd_pcm_t *mPlaybackHandle;

    int64_t mDelay2RenderArg;

    int mSampleRate;

    double mSilentTime;
    double mLastTimePutIn;
};
}
#endif
